/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EnviarCorreo.java
 *
 * Created on 31/05/2009, 01:15:56 AM
 */

package presentacion;

import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import negocio.control.AdministradoraListasDeCorreos;
import negocio.control.ConfiguradoraServidorSMTP;
import negocio.control.ProgramadoraDeEnvios;
import negocio.control.ServiciosDeCorreo;
import negocio.entidades.ListaDeCorreos;
import negocio.entidades.ServidorSMTP;

/**
 *
 * @author Administrador
 */
public class EnviarCorreo extends javax.swing.JFrame {

    /** Creates new form EnviarCorreo */
    public EnviarCorreo() {
        initComponents();
        inicializarListas();
    }

    public EnviarCorreo(IGEnvioDeCorreo parent) {
        this.parent = parent;
        initComponents();
        inicializarListas();
    }

    public EnviarCorreo(IGEnvioDeCorreo parent, String lista, String servidor){
        this.parent = parent;
        initComponents();
        inicializarListas();

        listas.setSelectedItem(lista);
        servidores.setSelectedItem(servidor);
    }

    public void inicializarListas(){
        LinkedList<ListaDeCorreos> listasC = ServiciosDeCorreo.consultarListasDeCorreo();
        LinkedList<ServidorSMTP> servicoresC = ServiciosDeCorreo.consultarServidoresSMTP();
        Iterator<ListaDeCorreos> listasCit = listasC.iterator();
        Iterator<ServidorSMTP> servicoresCit = servicoresC.iterator();

        while (listasCit.hasNext()) {
            ListaDeCorreos listaDeCorreos = listasCit.next();
            listas.addItem(listaDeCorreos.getNombre());
        }

        while (servicoresCit.hasNext()) {
            ServidorSMTP servidorSMTP = servicoresCit.next();
            servidores.addItem(servidorSMTP.getCorreoRemitente());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        servidores = new javax.swing.JComboBox();
        enviarAhora = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        enviarDespues = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        dia = new javax.swing.JTextField();
        mes = new javax.swing.JTextField();
        año = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        periodo = new javax.swing.JSpinner();
        jLabel10 = new javax.swing.JLabel();
        enviar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();
        listas = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("ENVÍO DE CORREOS");

        jLabel2.setText("Listado de Correos:");

        jLabel3.setText("Listado de SMTP:");

        buttonGroup1.add(enviarAhora);
        enviarAhora.setText("Enviar Ahora");
        enviarAhora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enviarAhoraActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        buttonGroup1.add(enviarDespues);
        enviarDespues.setText("Enviar Después");
        enviarDespues.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enviarDespuesActionPerformed(evt);
            }
        });

        jLabel4.setText("Fecha:");

        dia.setEditable(false);

        mes.setEditable(false);

        año.setEditable(false);

        jLabel6.setText("dd/mm/aaaa");

        jLabel7.setText("/");

        jLabel8.setText("/");

        jLabel9.setText("Periodicidad:");

        periodo.setModel(new javax.swing.SpinnerNumberModel(0, 0, 365, 1));
        periodo.setEnabled(false);

        jLabel10.setText("Días");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(jLabel4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(5, 5, 5)
                                .addComponent(jLabel7)
                                .addGap(9, 9, 9)
                                .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addComponent(jLabel8)
                                .addGap(8, 8, 8)
                                .addComponent(año, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(periodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel10))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(enviarDespues)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(enviarDespues)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(año, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(periodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)))
        );

        enviar.setText("Enviar");
        enviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enviarActionPerformed(evt);
            }
        });

        cancelar.setText("Cancelar");
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel3)
                                            .addComponent(enviarAhora)
                                            .addComponent(jLabel2))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(listas, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(servidores, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(enviar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cancelar)
                                        .addGap(12, 12, 12)))
                                .addGap(38, 38, 38))
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(110, 110, 110)
                        .addComponent(jLabel1)))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(listas, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(servidores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addComponent(enviarAhora)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(enviar)
                    .addComponent(cancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void enviarDespuesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enviarDespuesActionPerformed
        

        dia.setEditable(true);
        mes.setEditable(true);
        año.setEditable(true);
        periodo.setEnabled(true);
}//GEN-LAST:event_enviarDespuesActionPerformed

    private void enviarAhoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enviarAhoraActionPerformed
       
        dia.setEditable(false);
        mes.setEditable(false);
        año.setEditable(false);
        periodo.setEnabled(false);
        int puerto = (Integer)(periodo.getValue());
        System.out.println("");
}//GEN-LAST:event_enviarAhoraActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelarActionPerformed

    private void enviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enviarActionPerformed
        ServidorSMTP servidor = ConfiguradoraServidorSMTP.getInstancia().buscar((String) servidores.getSelectedItem());
        ListaDeCorreos lista = AdministradoraListasDeCorreos.getInstancia().buscar((String) listas.getSelectedItem());
        boolean resultado = false;
        if (enviarAhora.isSelected()) {
            resultado = ServiciosDeCorreo.enviarLista(lista, servidor);
        }

        if (enviarDespues.isSelected()) {
            Calendar c = Calendar.getInstance();
            c.set(Integer.parseInt(año.getText()), Integer.parseInt(mes.getText())-1, Integer.parseInt(dia.getText()),0,0,0);
            System.out.println(c.getTime());
            if((Integer)periodo.getValue() == 0){
                resultado = ProgramadoraDeEnvios.getInstancia().programarEnvio(lista, servidor, c.getTime());
            }else{
                resultado = ProgramadoraDeEnvios.getInstancia().programarEnvio(lista, servidor, c.getTime(),(Integer)periodo.getValue()*24*60*60*1000);
            }
        }

        if( ! resultado ){
            JOptionPane.showMessageDialog(this,
                    "No fue posible enviar el correo, por favor, intente mas tarde.",
                    "Error Enviar el correo",
                    JOptionPane.ERROR_MESSAGE);
        }
        parent.actualizarTabla();
        this.dispose();
    }//GEN-LAST:event_enviarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        try
        {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception ex)
        {
            //Sin comentarios
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EnviarCorreo().setVisible(true);
            }
        });
    }

    //Variables de clase
    private IGEnvioDeCorreo parent;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField año;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelar;
    private javax.swing.JTextField dia;
    private javax.swing.JButton enviar;
    private javax.swing.JRadioButton enviarAhora;
    private javax.swing.JRadioButton enviarDespues;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox listas;
    private javax.swing.JTextField mes;
    private javax.swing.JSpinner periodo;
    private javax.swing.JComboBox servidores;
    // End of variables declaration//GEN-END:variables

}
